import { defineStore } from "pinia";
import type { StuStore } from "./types/type";
import type { addStuResponseData, deleteStuResponseData, pageData, pageStuResponseData, studentInfoData, totalData } from "@/api/student/type";
import { reqAddStu, reqDeleteStu, reqFindById, reqPageStu, reqUpdateStu } from "@/api/student";


const useStuStore = defineStore("Student",{
    state:():StuStore=>{
        return{
            
        }
    },
    actions:{
        async addStu(data:studentInfoData){
            const result:addStuResponseData = await reqAddStu(data);
            if(result.status == 0){
                return result.data as studentInfoData;
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async FindById(_id:string){
            const result:addStuResponseData = await reqFindById(_id);
            if(result.status == 0){
                return result.data as studentInfoData;
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async updateStu(data:studentInfoData){
            const result:addStuResponseData = await reqUpdateStu(data);
            if(result.status == 0){
                return result.data as studentInfoData;
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async pageStu(data:pageData){
            const result:pageStuResponseData = await reqPageStu(data);
            if(result.status == 0){
                return result.data as totalData;
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async deleteStu(_id:string){
    
            
            const result:deleteStuResponseData = await reqDeleteStu(_id);
            if(result.status == 0){
                return "ok";
            }else{
                return Promise.reject(new Error(result.msg))
            }
        }
    }
})

export default useStuStore